﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using HeritageAcademy.Models.ViewModels;

namespace HeritageAcademy.Models.Filters
{
    public abstract class CourseFilter : INotifyPropertyChanged
    {
        #region Parameters 
        
        private bool _isFirstInStack;
        private bool _isLastInStack;
        private LibraryViewModel _library;
        private List<CourseViewModel> _results;
        
        #endregion

        #region Ctor 

        protected CourseFilter(LibraryViewModel library)
        {
            Library = library;
        }
        
        #endregion

        #region Properties 
        
        public abstract string DisplayText { get; }

        public abstract bool HasSubFilters { get; }

        public bool IsFirstInStack
        {
            get { return _isFirstInStack; }
            internal set
            {
                _isFirstInStack = value;
                RaisePropertyChanged("IsFirstInStack");
            }
        }

        public bool IsLastInStack
        {
            get { return _isLastInStack; }
            internal set
            {
                _isLastInStack = value;
                RaisePropertyChanged("IsLastInStack");
            }
        }

        public LibraryViewModel Library
        {
            get { return _library; }
            set
            {
                _library = value;
                RaisePropertyChanged("Library");
            }
        }

        public List<CourseViewModel> Results
        {
            get { return _results; }
            set
            {
                _results = value;
                RaisePropertyChanged("Results");
                RaiseResultsChanged();
            }
        }
        
        #endregion 

        #region Public Methods 
        
        public abstract void RefreshResults();

        public override string ToString()
        {
            return !string.IsNullOrEmpty(DisplayText) ? DisplayText : base.ToString();
        }

        public event EventHandler ResultsChanged;

        private void RaiseResultsChanged()
        {
            if (ResultsChanged != null)
            {
                ResultsChanged(this, new EventArgs());
            }
        }
        
        #endregion

        #region INotifyPropertyChanged Members
        
        public event PropertyChangedEventHandler PropertyChanged;
        
        protected void RaisePropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        
        #endregion
    }

    public abstract class FilteredCourseFilter : CourseFilter
    {
        #region Parameters 
        
        private CourseFilter _selectedSubFilter;
        
        #endregion

        #region Ctor 

        protected FilteredCourseFilter(LibraryViewModel library) : base(library)
        {
        }
        
        #endregion 

        #region Properties 
        
        public CourseFilter SelectedSubFilter
        {
            get { return _selectedSubFilter; }
            set
            {
                _selectedSubFilter = value;
                RefreshResults();
                RaisePropertyChanged("SelectedSubFilter");
            }
        }

        public abstract List<CourseFilter> SubFilters { get; }
        
        #endregion

        #region Public Methods 
        
        public override void RefreshResults()
        {
            if (SelectedSubFilter == null) return;
            SelectedSubFilter.RefreshResults();
            Results = SelectedSubFilter.Results;
        }

        public abstract void RefreshSubFilters();
        
        #endregion 
    }
}